﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/*
* University of Reykjavík
* Brjóstagjöf.is is the product of first-year practical programming course
* for computer science students at said university
*
* URL: http://code.google.com/p/scags 
* Authors: 
* Ástvaldur Sigurðsson
* Guðjón Jónsson
* Kristín Inga Vigfúsdóttir
* Steinar Bragi Sigurðsson
* Sveinn Ernstsson
*/

namespace skags.Models
{
    public class PollRepository
    {
        /* Mynda tengingu við grunninn okkar */
        readonly DBDataContext _dc = new DBDataContext();

        /* 
         * Fall sem vistar breytingar 
         */
        public void Save()
        {
            _dc.SubmitChanges();
        }

        /* 
         * Ná í eina könnun 
         * Notað í AdminController
         */
        public Poll GetPollById(int id)
        {
            return _dc.Polls.SingleOrDefault(p => p.ID == id);
        }

        /* 
         * Fall sem naer í allar kannanir 
         * Notað af Admin- og PollController
         */
        public IEnumerable<Poll> GetAllPolls()
        {
            var result = _dc.Polls.OrderByDescending(p => p.ID);
            return result;
        }

        /*
         * Fall sem naer í kannanir eftir númeri.
         * Notað í AdminController.
         */ 
        public Poll GetPollByID(int id)
        {
            return _dc.Polls.SingleOrDefault(u => u.ID == id);
        }

        /* 
         * Fall sem býr til könnun.
         * Notað AdminController.
         */
        public void CreatePoll(Poll poll)
        {
            var userRep = new UserRepository();
            poll.CreatorID = userRep.GetHashIDByUsername(HttpContext.Current.User.Identity.Name);
            poll.DateCreated = DateTime.Now;
            _dc.Polls.InsertOnSubmit(poll);
            Save();
        }

        /* 
         * Uppfærir könnun.
         * Notað í AdminController.
         */ 
        public void UpdatePoll(Poll item)
        {
            var thePoll = GetPollByID(item.ID);
            if (thePoll != null)
            {
                thePoll.Question = item.Question;
                thePoll.Option1 = item.Option1;
                thePoll.Option2 = item.Option2;
                thePoll.Option3 = item.Option3;
                thePoll.Option4 = item.Option4;
                thePoll.Option5 = item.Option5;

                Save();
            }
        }

        /*
         * Fall sem eyðir könnun
         * Notað í AdminController
         */         
        public void RemovePoll(int id)
        {
            var pollToRemove = GetPollById(id);
            if (pollToRemove != null) _dc.Polls.DeleteOnSubmit(pollToRemove);
            Save();
        }

        /*
         * Saekir nýjustu könnun
         * Notað við PollController
         */ 
        public PollModels GetLatestPoll()
        {
            //var poll = (_dc.Polls.OrderByDescending(p => p.ID)).FirstOrDefault();

            var poll = (from s in _dc.Polls
                        orderby s.ID descending
                        select new PollModels
                                   {
                                       CreatorID = s.CreatorID,
                                       DateCreated = s.DateCreated,
                                       id = s.ID,
                                       Option1 = s.Option1,
                                       Option2 = s.Option2,
                                       Option3 = s.Option3,
                                       Option4 = s.Option4,
                                       Option5 = s.Option5,
                                       Question = s.Question,
                                       Vote1 = (s.Vote1.HasValue) ? s.Vote1.Value : 0,
                                       Vote2 = (s.Vote2.HasValue) ? s.Vote2.Value : 0,
                                       Vote3 = (s.Vote3.HasValue) ? s.Vote3.Value : 0,
                                       Vote4 = (s.Vote4.HasValue) ? s.Vote4.Value : 0,
                                       Vote5 = (s.Vote5.HasValue) ? s.Vote5.Value : 0

                                   }).FirstOrDefault();
            
            return poll;
        }

        /*
         * Ná í nýjustu könnun
         * Notað í PollController.
         */ 
        public PollModels GetNewestPoll()
        {

            var poll = (from s in _dc.Polls
                        orderby s.ID descending
                        select new PollModels
                        {
                            CreatorID = s.CreatorID,
                            DateCreated = s.DateCreated,
                            id = s.ID,
                            Option1 = s.Option1,
                            Option2 = s.Option2,
                            Option3 = s.Option3,
                            Option4 = s.Option4,
                            Option5 = s.Option5,
                            Question = s.Question,
                            Vote1 = (s.Vote1.HasValue) ? s.Vote1.Value : 0,
                            Vote2 = (s.Vote2.HasValue) ? s.Vote2.Value : 0,
                            Vote3 = (s.Vote3.HasValue) ? s.Vote3.Value : 0,
                            Vote4 = (s.Vote4.HasValue) ? s.Vote4.Value : 0,
                            Vote5 = (s.Vote5.HasValue) ? s.Vote5.Value : 0

                        }).FirstOrDefault();

            return poll;
        }

        /*
         * Uppfaerir atkvaeðagreiðslu fyrir könnun.
         * Notað í Admin- og PollController.
         */ 
        public void UpdateVote(string votecolumn)
        {
            //get PollID - Þetta skilar samt bara nýjustu könuninni, ekki current
            var myID = (from s in _dc.Polls
                        orderby s.ID descending
                        select s).FirstOrDefault();

            //get current vote number
            
            if(votecolumn == "Vote1")
            {
                var cur = myID.Vote1;
                if( cur == null )
                {
                    cur = 1;
                }
                else
                {
                    cur++;
                }
                myID.Vote1 = cur;
            }

            if (votecolumn == "Vote2")
            {
                var cur = myID.Vote2;
                if (cur == null)
                {
                    cur = 1;
                }
                else
                {
                    cur++;
                }
                myID.Vote2 = cur;
            }

            if (votecolumn == "Vote3")
            {
                var cur = myID.Vote3;
                if (cur == null)
                {
                    cur = 1;
                }
                else
                {
                    cur++;
                }
                myID.Vote3 = cur;
            }

            if (votecolumn == "Vote4")
            {
                var cur = myID.Vote4;
                if (cur == null)
                {
                    cur = 1;
                }
                else
                {
                    cur++;
                }
                myID.Vote4 = cur;
            }

            if (votecolumn == "Vote5")
            {
                var cur = myID.Vote5;
                if (cur == null)
                {
                    cur = 1;
                }
                else
                {
                    cur++;
                }
                myID.Vote5 = cur;
            }

            Save();
        }
    }
}